﻿//JumbotCms_4.1 Final
using System;
using System.Data;
using System.Web;
using System.Web.UI.WebControls;
using Jumbot.Common;
using JumbotCms.Common;
namespace JumbotCms.WebFile.Admin
{
    public partial class _user_applyajax : JumbotCms.API.AdminCenter
    {
        private string _operType = string.Empty;
        private string _response = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!CheckFormUrl())
            {
                Response.Write("提交有误");
                Response.End();
            }
            Admin_Load("0000", "json");
            this._operType = q("oper");
            switch (this._operType)
            {
                case "ajaxGetList":
                    ajaxGetList();
                    break;
                case "check":
                    ajaxCheck();
                    break;
                case "ajaxDel":
                    ajaxDel();
                    break;
                default:
                    DefaultResponse();
                    break;
            }
            Response.Write(this._response);
        }

        private void DefaultResponse()
        {
            this._response = JsonResult(0, "未知操作");
        }
        private void ajaxGetList()
        {
            int page = Int_ThisPage();
            int PSize = Str2Int(q("pagesize"), 20);
            int countNum = 0;
            string sqlStr = "";
            string joinStr = "A.[UserId]=B.Id";
            string whereStr1 = "A.Id>0";//外围条件(带A.)
            string whereStr2 = "Id>0";//分页条件(不带A.)
            doh.Reset();
            doh.ConditionExpress = whereStr2;
            countNum = doh.Count("jcms_normal_user_apply");
            sqlStr = Jumbot.Common.staticFunction.GetSql("A.id as id,A.UserId as UserId,B.UserName as UserName,A.ApplyInfo as ApplyInfo,A.ApplyType as ApplyType,A.ApplyTime as ApplyTime,A.Checked as Checked", "jcms_normal_user_apply", "jcms_normal_user", "Id", PSize, page, "desc", joinStr, whereStr1, whereStr2);
            doh.Reset();
            doh.SqlCmd = sqlStr;
            DataTable dt = doh.GetDataTable();
            this._response = "{result :\"1\"," +
                "returnval :\"操作成功\"," +
                "pagerbar :\"" + Jumbot.Common.HtmlPager.GetPageBar(3, "js", 2, countNum, PSize, page, "javascript:ajaxList(<#page#>);") + "\"," +
                Jumbot.Common.JsonHelp.DtToSON(dt, (PSize * (page - 1))) +
                "}";
            dt.Clear();
            dt.Dispose();
        }
        private void ajaxCheck()
        {
            string aId = f("id");
            doh.Reset();
            doh.ConditionExpress = "id=@id and Checked=0";
            doh.AddConditionParameter("@id", aId);
            object[] _value = doh.GetValues("jcms_normal_user_apply", "UserId,ApplyType,ApplyNumber");
            string _ApplyUserId = Str2Str(_value[0].ToString());
            //1=申请Writer,2=购买点数,3=包年,4=提现
            string _ApplyType = Str2Str(_value[1].ToString());

            int _ApplyNumber = Str2Int(_value[2].ToString());

            JumbotCms.DataObject.User _User = new JumbotCms.DataObject.User();
            string _uName = _User.GetUserName(_ApplyUserId, doh);
            switch (_ApplyType)
            {
                case "1":
                    _User.Change2Writer(_ApplyUserId, doh);
                    SaveAdminLog("将[" + _uName + "]升级为了" + site.WriterItem);
                    SaveUserLog(_ApplyUserId, AdminName + "将你升级为了" + site.WriterItem, 1);
                    break;
                case "2":
                    _User.AddPoints(_ApplyUserId, _ApplyNumber, doh);
                    SaveAdminLog("给[" + _uName + "]充点数:" + _ApplyNumber);
                    SaveUserLog(_ApplyUserId, AdminName + "给你充了" + _ApplyNumber + "点数", 4);
                    break;
                case "3":
                    _User.AddVIPYears(_ApplyUserId, _ApplyNumber, doh);
                    SaveAdminLog("给[" + _uName + "]包了" + _ApplyNumber + "年VIP服务");
                    SaveUserLog(_ApplyUserId, AdminName + "给你包了" + _ApplyNumber + "年VIP服务", 5);
                    break;
                case "4":
                    _User.SubstractIntegral(_ApplyUserId, _ApplyNumber, doh);
                    SaveAdminLog("给[" + _uName + "]扣积分:" + _ApplyNumber);
                    SaveUserLog(_ApplyUserId, AdminName + "给你扣除" + _ApplyNumber + "积分", 4);
                    break;
                default:
                    break;
            }
            doh.Reset();
            doh.ConditionExpress = "id=@id and Checked=0";
            doh.AddConditionParameter("@id", aId);
            doh.Audit("jcms_normal_user_apply", "Checked");//先审核
            this._response = JsonResult(1, "成功操作");
        }
        private void ajaxDel()
        {
            string aId = f("id");
            doh.Reset();
            doh.ConditionExpress = "id=@id and Checked=0";
            doh.AddConditionParameter("@id", aId);
            if (doh.Delete("jcms_normal_user_apply") == 1)
                this._response = JsonResult(1, "成功删除");
            else
                this._response = JsonResult(0, "删除有误");
        }
    }
}